﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using MyDataProvider;
using Cfg;
namespace ExcelDbAddIn
{
    public partial class FormConnection : Form
    {
        SqlConnectionStringBuilder sqlConnString;
        SqlConnectionConfig _config = new SqlConnectionConfig();     

        public FormConnection()
        {
            InitializeComponent();
        }
        public void SetConfig(SqlConnectionConfig cfg)
        {
            _config.Auth = new MSSQL.Authorization()
            {
                Type = cfg.Auth.Type,
                UserId = cfg.Auth.UserId,
                Password = cfg.Auth.Password
            };
            _config.DataSource = cfg.DataSource;
        }
        public SqlConnectionConfig GetConfig()
        {
            SqlConnectionConfig cfg = new SqlConnectionConfig();
            cfg.Auth = new MSSQL.Authorization()
            {
                Type = _config.Auth.Type,
                UserId = _config.Auth.UserId,
                Password = _config.Auth.Password
            };
            cfg.DataSource = _config.DataSource;
            return cfg;
        }
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
        
        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                this.DialogResult = DialogResult.OK;
                this.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw ex;
            }

        }

        private void comboBoxAuthType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string auth = comboBoxAuthType.SelectedItem.ToString();
            textBoxPassword.Text = "";
            textBoxUserId.Text = "";
            if (auth == "Использовать проверку SQL Server")
            {
                _config.Auth.Type = MSSQL.Authorization.AuthorizationType.SQLAuth;
                labelUserId.Enabled = true;
                textBoxUserId.Enabled = true;
                labelPassword.Enabled = true;
                textBoxPassword.Enabled = true;
            }
            else
            {
                _config.Auth.Type = MSSQL.Authorization.AuthorizationType.WindowsAuth;
                labelUserId.Enabled = false;
                textBoxUserId.Enabled = false;
                labelPassword.Enabled = false;
                textBoxPassword.Enabled = false;
            }
            buttonCheckConnection.Enabled = true;
        }

        private void buttonCheckConnection_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBoxServer.TextLength == 0)
                {
                    MessageBox.Show("Введите имя сервера", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (_config.Auth.Type == MSSQL.Authorization.AuthorizationType.None)
                {
                    MessageBox.Show("Выберите способ аутентификации", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (_config.Auth.Type == MSSQL.Authorization.AuthorizationType.SQLAuth && textBoxUserId.TextLength == 0)
                {
                    MessageBox.Show("Введите имя пользователя", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (_config.Auth.Type == MSSQL.Authorization.AuthorizationType.SQLAuth && textBoxPassword.TextLength == 0)
                {
                    MessageBox.Show("Введите пароль", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _config.DataSource= textBoxServer.Text;
                if (_config.Auth.Type == MSSQL.Authorization.AuthorizationType.SQLAuth)
                {
                    _config.Auth.UserId = textBoxUserId.Text;
                    _config.Auth.Password = textBoxPassword.Text;
                }
                sqlConnString = MSSQL.GetConnectionString(_config);
                bool checkResult = MSSQL.CheckConnection(sqlConnString);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw ex;
            }
            MessageBox.Show("Проверка подключения выполнена", "Настройка подключения",MessageBoxButtons.OK, MessageBoxIcon.Information);
            buttonOK.Enabled = true;
        }        

       
        private void FormConnection_Load(object sender, EventArgs e)
        {            
            comboBoxAuthType.SelectedIndexChanged -= comboBoxAuthType_SelectedIndexChanged;
            if (_config.Auth.Type != MSSQL.Authorization.AuthorizationType.None)
                buttonCheckConnection.Enabled = true;
            if (_config.Auth.Type == MSSQL.Authorization.AuthorizationType.SQLAuth)
            {
                // "Использовать проверку SQL Server";
                comboBoxAuthType.SelectedIndex = 1;
                textBoxUserId.Text = _config.Auth.UserId;
                textBoxUserId.Enabled = true;
                textBoxPassword.Text = _config.Auth.Password;
                textBoxPassword.Enabled = true;
            }
            else
                if (_config.Auth.Type== MSSQL.Authorization.AuthorizationType.WindowsAuth)
                {
                    // // "Использовать проверку подлинности Windows";
                    comboBoxAuthType.SelectedIndex = 0;
                    textBoxUserId.Enabled = false;
                    textBoxPassword.Enabled = false;
                }
            textBoxServer.Text = _config.DataSource;         
            comboBoxAuthType.SelectedIndexChanged += comboBoxAuthType_SelectedIndexChanged;
        }
    }
}
